<!--购物车页面-->
<template>
  <div class="shop-car">
    <van-nav-bar
      title="购物车"
      fixed
    />
    <div class="card-title">
      <van-button size="small" type="danger" @click="clearShopCar" plain>清空购物车</van-button>
    </div>
    <!-- 购物车中的商品 -->
    <div class="card-list">
      <div class="goods-row" v-for="(item, index) in carInfo" :key="index">
        <div class="goods-img"><img :src="item.image" width="100%" onerror="this.src='http://cs-static-assets.oss-cn-beijing.aliyuncs.com/ds_system/errorimg.png'" /></div>
        <div class="goods-desc">
            <div class="goods-name">{{item.name}}</div>
            <div class="goods-control">
                <van-stepper v-model="item.count" min="0" @change="substractGoods" />
            </div>
        </div>
        <div class="goods-price">
          <div>￥{{item.price | toMoney}}</div>
          <div> X {{item.count}}</div>
          <div class="allPrice">
            ￥{{(item.price*item.count) | toMoney}}
          </div>
        </div>
      </div>
    </div>
    <!-- 显示商品总价 -->
    <div class="totalMoney">商品总价：￥ {{totalMoney | toMoney}}</div>
  </div>
</template>

<script>
import toMoney from '../../filter/moneyFilter';
export default {
  data() {
    return {
      carInfo: JSON.parse(localStorage.getItem('carInfo')) || [],
    }
  },
  methods: {
    clearShopCar() {
      localStorage.setItem('carInfo', JSON.stringify([]));
      this.carInfo = [];
    },
    // 方案一，使用的时候totalMoney()
    // totalMoney() {
    //   let total = 0;
    //   this.carInfo.forEach(element => {
    //     total += element.price * element.count;
    //   });
    //   localStorage.setItem('carInfo', JSON.stringify(this.carInfo));
    //   return total;
    // }
    substractGoods() {
      //  这里必须使用change事件，当绑定值变化时触发的事件，如果使用minus按钮事件，数据会产生延迟
      const filterGoods = this.carInfo.filter((item) => {
        return item.count > 0;
      })
      localStorage.setItem('carInfo', JSON.stringify(filterGoods));
      this.carInfo = filterGoods;
    }
  },
  filters: {
    toMoney(value) {
      return toMoney(value);
    }
  },
  // 方案二,使用计算属性,watch是监听组件之间传递过来的属性，computed是动态计算自身的属性值（要有return返回值）,不用在data中再次声明改属性了，否则会报警告
  computed: {
    totalMoney() {
      let total = 0;
      this.carInfo.forEach(element => {
        total += element.price * element.count;
      });
      // 因为stepper的v-model:item.count,所以会动态更新this.carInfo的count属性值
      localStorage.setItem('carInfo', JSON.stringify(this.carInfo));
      return total;
    }
  }
};
</script>

<style scoped>
  .card-title {
    height: 2rem;
    line-height: 2rem;
    border-bottom: 1px solid #E4E7ED;
    background-color: #fff;
    padding: 5px;
    text-align: right;
    margin-top: 46px;
  }
  .card-list {
    background-color: #fff;
  }
  .goods-row {
    display: flex;
    flex-wrap: nowrap;
    padding: .5rem;
    font-size: .85rem;
    border-bottom: 1px solid #e4e7ed;
  }
  .goods-img{
    flex: 6;
  }
  .goods-desc{
    flex: 14;
    padding-left: 10px;
  }
  .goods-control {
    padding-top: 10px;
  }
  .goods-price {
    flex: 4;
    text-align: right;
  }
  .allPrice {
    color: lightgreen;
  }
  .totalMoney {
    margin-bottom: 50px;
    color: red;
    background-color: #fff;
    padding: 10px;
  }
</style>